package com.icefish.control;


import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.fastjsonParse.img.Img;
import com.icefish.utils.Init_Req_Resp_Config;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@WebServlet("/UserPicture")
public class UserPicture extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Init_Req_Resp_Config.init(req, resp);

        // 创建磁盘项目工厂
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

        // 创建解析类
        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

        try {
            // 获取请求中的属性
            List<FileItem> list = servletFileUpload.parseRequest(req);

            // 循环遍历属性中的值
            for (FileItem fileItem : list) {
                // 判断是否为文件属性
                if (fileItem.isFormField()) {
                    String name = fileItem.getFieldName();
                    String value = fileItem.getString();

                    System.out.println(name + "-" + value);
                } else {
                    // 获取文件的文件名
                    String fileName = fileItem.getName();
                    // 获取文件大小
                    long fileSize = fileItem.getSize();

                    System.out.println("文件名为" + fileName + ",文件大小为" + fileSize);

                    // 获取文件的输入流
                    InputStream inputStream = fileItem.getInputStream();

                    // 创建存放文件的路径
                    String path = getServletContext().getRealPath("/users_picture");
                    System.out.println(path);

                    // 创建输出流
                    OutputStream outputStream = new FileOutputStream(path + "\\" + fileName);
                    int len = 0;
                    byte[] bytes = new byte[1024];
                    while ((len = inputStream.read(bytes)) != -1) {
                        // 输出流写入文件
                        outputStream.write(bytes, 0, len);
                    }

                    // 关闭输入输出流
                    inputStream.close();
                    outputStream.close();

                    // 返回图片url路径
                    String data = "http://localhost:8888" + req.getContextPath() + "/users_picture/" + fileName;
                    Img img = new Img(data);

                    JSONObject jsonObject = new JSONObject();
                    // 转换成json
                    String backToBrowser = jsonObject.toJSONString(img);

                    resp.getWriter().println(backToBrowser);
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

}

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
